﻿using EShop.Models;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace EShop.Controllers
{
    public class ShopManagerController : Controller
    {
        EShop.Models.EShopEntities shopDB
            = new EShop.Models.EShopEntities();
        //
        // GET: /ShopManager/

        public ActionResult Index()
        {
            var goods = shopDB.Goods.Include("Genre").Include("brand");
            return View(goods.ToList());
        }

        //
        // GET: /ShopManager/Details/5

        public ViewResult Details(int id)
        {
            EShop.Models.Goods goods = shopDB.Goods.Find(id);
            return View(goods);
        }

        //
        // GET: /ShopManager/Create

        public ActionResult Create()
        {
            ViewBag.GenreId = new SelectList(shopDB.Genres, "GenreId", "Name");
            ViewBag.brandId = new SelectList(shopDB.brands, "brandId", "Name");
            return View();
        }

        //
        // POST: /ShopManager/Create

        [HttpPost]
        public ActionResult Create(Goods goods)
        {
            if (ModelState.IsValid)
            {
                shopDB.Goods.Add(goods);
                shopDB.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.GenreId = new SelectList(shopDB.Genres, "GenreId", "Name", goods.GenreId);
            ViewBag.brandId = new SelectList(shopDB.brands, "brandId", "Name", goods.brandId);
            return View(goods);
        }

        //
        // GET: /ShopManager/Edit/5

        public ActionResult Edit(int id)
        {
            Goods goods = shopDB.Goods.Find(id);
            ViewBag.GenreId = new SelectList(shopDB.Genres, "GenreId", "Name", goods.GenreId);
            ViewBag.brandId = new SelectList(shopDB.brands, "brandId", "Name", goods.brandId);
            return View(goods);
        }

        //
        // POST: /ShopManager/Edit/5

        [HttpPost]
        public ActionResult Edit(Goods goods)
        {
            try
            {
                // TODO: Add update logic here
                ViewBag.GenreId = new SelectList(shopDB.Genres, "GenreId", "Name", goods.GenreId);
                ViewBag.brandId = new SelectList(shopDB.brands, "brandId", "Name", goods.brandId);
                if (ModelState.IsValid)
                {
                    shopDB.Entry(goods).State = EntityState.Modified;
                    shopDB.SaveChanges();
                    return RedirectToAction("Index");
                }
                
                return View(goods);
            }
            catch
            {
                return View(goods);
            }
        }

        //
        // GET: /ShopManager/Delete/5

        public ActionResult Delete(int id)
        {
            EShop.Models.Goods goods = shopDB.Goods.Find(id);
            return View(goods);
        }

        //
        // POST: /ShopManager/Delete/5

            [HttpPost, ActionName("Delete")]
            public ActionResult DeleteConfirmed(int id)
            {            
                Goods goods= shopDB.Goods.Find(id);
                shopDB.Goods.Remove(goods);
                shopDB.SaveChanges();
                return RedirectToAction("Index");
            }

            protected override void Dispose(bool disposing)
            {
                shopDB.Dispose();
                base.Dispose(disposing);
            }
    }
}
